package com.xwj.config;

import com.xwj.data.StaticData;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by xwj on 2017/7/13.
 * rabbitmq
 * 为了保证在RabbitMQ退出或者crash了数据仍没有丢失，需要将queue和Message都要持久化 durable为true
 */
@Configuration
public class AmqpConfig {

    @Bean(name = "topicAddOrderQueue")
    public Queue topicAddOrderQueue() {
        return new Queue(StaticData.AMQP_MQ_QUEUE_TOPIC_ADD_ORDER, true);
    }

    @Bean(name = "topicOrderExchange")
    public TopicExchange topicOrderExchange() {
        return new TopicExchange(StaticData.AMQP_MQ_ORDER_EXCHANGE);
    }

    @Bean
    public Binding bindingExchangeTopicAddOrderQueue(@Qualifier("topicAddOrderQueue") Queue topicAddOrderQueue,
                                                     @Qualifier("topicOrderExchange") TopicExchange topicOrderExchange) {
        return BindingBuilder.bind(topicAddOrderQueue).to(topicOrderExchange).with(StaticData.AMQP_MQ_QUEUE_TOPIC_ADD_ORDER);
    }


}
